import axios from '@ohos/axios'
import Account from '../viewModel/Account'
import AccountType from '../viewModel/AccountType'
import User from '../viewModel/User'
// let baseUrl= '10.2.2.108'
let baseUrl= '192.168.43.254'
class AccountModel{
  //查询当月
  getAccountList():Promise<Account[]>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'GET',
        // url:'http://localhost:8080/account/listMonthAccount',
        url:'http://'+baseUrl+':8080/account/listMonthAccount',
        params:{
          date:AppStorage.Get('date'),
          uid:AppStorage.Get('uid')
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('查询失败')
      })
    })
  }
  //收入排行
  getInAccountList():Promise<Account[]>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'GET',
        // url:'http://localhost:8080/account/listInMonthAccount',
        url:'http://'+baseUrl+':8080/account/listInMonthAccount',
        params:{
          date:AppStorage.Get('date'),
          uid:AppStorage.Get('uid')
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('查询失败')
      })
    })
  }
  //支出排行
  getOutAccountList():Promise<Account[]>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'GET',
        // url:'http://localhost:8080/account/listOutMonthAccount',
        url:'http://'+baseUrl+':8080/account/listOutMonthAccount',
        params:{
          date:AppStorage.Get('date'),
          uid:AppStorage.Get('uid')
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('查询失败')
      })
    })
  }
  //查询全年
  allAccount():Promise<Account[]>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'GET',
        // url:'http://localhost:8080/account/allAccount',
        url:'http://'+baseUrl+':8080/account/allAccount',
        params:{
          date:AppStorage.Get('date'),
          uid:AppStorage.Get('uid')
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('查询失败')
      })
    })
  }
  //删除
  deleteRecord(account:Account):Promise<number>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'POST',
        // url:'http://localhost:8080/account/deleteAccount',
        url:'http://'+baseUrl+':8080/account/deleteAccount',
        data:{
          aid:account.aid,
          isdelte:1,
          flag:account.flag,
          date:account.date,
          uid:account.uid
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('失败')
      })
    })
  }
  //新增
  insertRecord(account:Account):Promise<number>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'POST',
        // url:'http://localhost:8080/account/insertAccount',
        url:'http://'+baseUrl+':8080/account/insertAccount',
        data:{
          aid:account.aid,
          atid:account.atid,
          uid:account.uid,
          isdelete:0,
          flag:account.flag,
          date:account.date,
          price:account.price,
          aname:account.aname
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('失败')
      })
    })
  }
  //记账总天数
  countDay():Promise<number>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'GET',
        // url:'http://localhost:8080/account/countDay',
        url:'http://'+baseUrl+':8080/account/countDay',
        params:{
          uid:AppStorage.Get('uid')
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('days::  ',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('失败')
      })
    })
  }
  //记账总笔数
  countAccount():Promise<number>{
    return new Promise((resolve,reject)=>{
      axios({
        method:'GET',
        // url:'http://localhost:8080/account/countAccount',
        url:'http://'+baseUrl+':8080/account/countAccount',
        params:{
          uid:AppStorage.Get('uid')
        },
        headers:{
          'Content-Type': 'application/json',
          'token':AppStorage.Get('token')
        }
      }).then(resp=>{
        if (resp.status == 200) {
          console.log('success',JSON.stringify(resp.data))
          resolve(resp.data)
        }
        else console.log('失败')
        reject('失败')
      })
    })
  }
}
const accountModel = new AccountModel();
export default accountModel as AccountModel;
